//
//  SNSStickerView.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-8-13.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "SNSStickerView.h"

#define kDefaultPosX    100
#define kDefaultPosY    100


@interface SNSStickerView()<UIGestureRecognizerDelegate>{
    CGFloat lastScale;
    CGFloat lastRotation;
    CGFloat translation;
}
@property(strong, nonatomic)UIImageView *imageView;

@end

@implementation SNSStickerView

- (id)initWithImage:(UIImage *)image
{
    _image = image;
    CGRect frame = CGRectMake(kDefaultPosX, kDefaultPosY, image.size.width, image.size.height);
    _pos = CGPointMake(kDefaultPosX, kDefaultPosY);
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _imageView = [[UIImageView alloc]initWithImage:image];
        [self addSubview:self.imageView];
        
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
        [self addGestureRecognizer:panGesture];
        panGesture.delegate = self;
        
        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scaleImage:)];
        [self addGestureRecognizer:pinchGesture];
        pinchGesture.delegate = self;
        
        UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)];
        [self addGestureRecognizer:rotationGesture];
        rotationGesture.delegate = self;
        
        
//        panGesture requireGestureRecognizerToFail:<#(UIGestureRecognizer *)#>

    }
    return self;
}

- (id)initWithImageName:(NSString *)imageName{
    UIImage *image = [UIImage imageNamed:imageName];
    return [self initWithImage:image];
    
}


- (void)handlePan:(UIPanGestureRecognizer *)sender {
    NSLog(@"UIPanGestureRecognizer = %@", sender);
    CGPoint translation = [sender translationInView:self.superview];
    self.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y);
    [sender setTranslation:CGPointZero inView: self];
    
    self.pos = self.frame.origin;
    
}

-(void)scaleImage:(UIPinchGestureRecognizer *)sender{
    NSLog(@"UIPinchGestureRecognizer = %@", sender);
    if([sender state] == UIGestureRecognizerStateEnded) {
        lastScale = 1.0;
        return;
    }
    
    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
    CGAffineTransform currentTransform = self.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    
    [self setTransform:newTransform];
    lastScale = [sender scale];
    
}

-(void)rotateImage:(UIRotationGestureRecognizer *)sender{
    NSLog(@"UIRotationGestureRecognizer = %@", sender);
    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        lastRotation = 0.0;
        return;
    }
    CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
    
    CGAffineTransform currentTransform = self.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
    
    [self setTransform:newTransform];
    
    lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

@end
